package com.molichuxing.services.infrastructure.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.CheckBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CheckMapper;
import com.molichuxing.services.infrastructure.dto.request.create.CheckCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.CarCheckModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.PdiCheckModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CheckDto;
import com.molichuxing.services.infrastructure.service.CheckService;
import com.molichuxing.services.property.CarCheckStatusEnum;
import com.molichuxing.services.property.CarsTypeEnum;

/**
 * <p>
 * 车辆检验表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("checkService")
public class CheckServiceImpl implements CheckService {

	@Resource
	private CheckMapper checkMapper;

	@Override
	public Paged<CheckDto> getCarsCheckListPage(List<Integer> carIds, CarsTypeEnum carsTypeEnum,
			CarCheckStatusEnum carCheckStatusEnum, LocalDateTime startCheckTime, LocalDateTime endCheckTime,
			Integer pageSize, Integer pageNum) {
		Paged<CheckDto> paged = new Paged<>();
		int count = checkMapper.getCheckPageCount(carIds.size() == 0 ? null : carIds,
				carsTypeEnum == null ? null : carsTypeEnum.getValue(),
				carCheckStatusEnum == null ? null : carCheckStatusEnum.getValue(), startCheckTime, endCheckTime);
		if (count <= 0) {
			return paged;
		}
		List<CheckBean> pageResult = checkMapper.getCheckPagePage(carIds.size() == 0 ? null : carIds,
				carsTypeEnum == null ? null : carsTypeEnum.getValue(),
				carCheckStatusEnum == null ? null : carCheckStatusEnum.getValue(), startCheckTime, endCheckTime,
				pageNum, pageSize);

		paged.setList(Convert.toCheckPageBeanDto(pageResult));
		paged.setTotal(count);
		paged.setPageSize(pageSize);
		return paged;
	}

	@Override
	public List<CheckDto> getCheckByCarId(Integer carId) {
		return Convert.toCheckPageBeanDto(checkMapper.getCheckByCarId(carId));
	}

	@Override
	public List<CheckDto> getCheckByOrderCode(Long orderCode) {
		return Convert.toCheckPageBeanDto(checkMapper.getCheckByOrderCode(orderCode));
	}

	@Override
	public CheckDto getById(Integer id) {
		return Convert.toCheckBeanDto(checkMapper.getById(id));
	}

	@Override
	public int pdiModifyCheckDto(PdiCheckModifyDto pdiCheckModifyDto) {
		CheckBean checkBean = checkMapper.getById(pdiCheckModifyDto.getCheckId());
		checkBean.setLossFee(pdiCheckModifyDto.getLossFee());
		checkBean.setRemark(pdiCheckModifyDto.getRemark());
		return checkMapper.modify(checkBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public int modifyCheckDto(Integer id, Integer facadeStatus, String remark, Integer carType,
			CarCheckModifyDto carCheckModifyDto) {
		CheckBean checkBean = checkMapper.getById(id);
		checkBean.setFacadeStatus(facadeStatus);
		checkBean.setRemark(remark);
		checkBean.setStatus(CarCheckStatusEnum.ALREADY_CHECK.getValue());
		checkBean.setCarCheckInfo(JSONObject.toJSONString(carCheckModifyDto));
		checkBean.setCarType(carType);
		checkBean.setCheckTime(LocalDateTime.now());

		if (carCheckModifyDto != null) {
			checkBean.setCarKeyNums(carCheckModifyDto.getCarKeyNums());
			checkBean.setMileage(carCheckModifyDto.getMileage());
		}
		return checkMapper.modify(checkBean);
	}

	@Override
	public Paged<CheckDto> getPdiCarsCheckListPage(List<Integer> carIds, Long orderCode, Integer returnStoreId,
			CarCheckStatusEnum carCheckStatusEnum, Integer dealerId, Integer pageSize, Integer pageNum) {
		Paged<CheckDto> paged = new Paged<>();
		int count = checkMapper.getPdiCheckPageCount(carIds.size() == 0 ? null : carIds, orderCode, returnStoreId,
				carCheckStatusEnum == null ? null : carCheckStatusEnum.getValue(), dealerId);
		if (count <= 0) {
			return paged;
		}
		List<CheckBean> pageResult = checkMapper.getPdiCheckPagePage(carIds.size() == 0 ? null : carIds, orderCode,
				returnStoreId, carCheckStatusEnum == null ? null : carCheckStatusEnum.getValue(), dealerId, pageNum,
				pageSize);
		paged.setList(Convert.toCheckPageBeanDto(pageResult));
		paged.setTotal(count);
		paged.setPageSize(pageSize);
		return paged;
	}


	/**
	 * 根据状态获取数量
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<Integer, Integer> getCheckCountByStatus(Integer checkType, Integer dealerId) throws Exception {
		List<TabDto> statusList = checkMapper.getCountByStatus(checkType, dealerId);
		if (null == statusList || statusList.size() == 0) {
			return null;
		}

		Map<Integer, Integer> returnMap = new HashMap<>();
		for (TabDto tabDto : statusList) {
			Integer type = tabDto.getType();
			Integer count = tabDto.getCount();
			returnMap.put(type, count);
		}

		return returnMap;
	}

	/**
	 * 全部数量
	 * 
	 * @return
	 */
	@Override
	public Integer getCount(Integer type, Integer dealerId) {
		return checkMapper.getCount(type, dealerId);
	}


	/**
	 * 检验待处理数量
	 * @param type
	 * @return
	 */
	@Override
	public Integer getPendCount(Integer type) {
		return checkMapper.getPendCount(type);
	}

	/**
	 * 修改检验信息
	 * @param
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean modifyCheckStatus(Integer id,Integer status) throws Exception {
		CheckBean checkBean = checkMapper.getById(id);
		if(null == checkBean){
			throw new BizException("检验信息不存在");
		}

		return (checkMapper.modifyCheckStatus(id,status) > 0) ? true : false;
	}

    @Override
    public Boolean modifyCheckStatusAndOpName(Integer id, Integer status, String opName) throws Exception {
		CheckBean checkBean = checkMapper.getById(id);
		if(null == checkBean){
			throw new BizException("检验信息不存在");
		}
		return (checkMapper.modifyCheckStatusAndOpName(id,status,opName) > 0) ? true : false;
    }

    /**
	 * 车辆检验新增
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public Boolean create(CheckCreateDto createDto) {
		checkMapper.create(Convert.toCheckBean(createDto));
		return true;
	}

	/**
	 * 查询
	 * 
	 * @param orderCodes
	 *            订单号
	 * @return
	 */
	@Override
	public Map<Long, CheckDto> getCheck(List<Long> orderCodes) {
		List<CheckBean> beans = checkMapper.getCheckList(orderCodes);
		if (beans == null || beans.isEmpty()) {
			return null;
		}
		return beans.stream()
				.collect(Collectors.toMap(CheckBean::getOrderCode, bean -> Convert.toCheckBeanDto(bean)));
	}

}
